/**
 * 请编写页面文件说明
 *
 * @author 填写作者
 * @file   company-page.component.ts
 * @date   2018-7-9 19:40:49
 */
import { Component, OnInit } from '@angular/core';
import { ToastService, ConfirmService, Pagination, ModalService } from 'ng-tui';
import { ApiData } from '../../../../cores/classes';
import { SearchParams } from '../../../../cores/classes/search-params';
import { GlobalService } from '../../../../cores/services';
import { RechargeService } from '../../service/recharge.service';
import { ActivatedRoute, Router } from '@angular/router';
import { skipWhile, switchMap, map } from 'rxjs/operators';
import { HttpConfig } from '../../../../configs/http.config';
import { ModalComponent } from './modal.component';
import { Recharge } from '../../interface/recharge.interfaces';
@Component({
    templateUrl: './company -type.component.html',
    styleUrls: ['./company -type.component.scss']
})
export class CompanyTypeComponent implements OnInit {

    source = HttpConfig.SERVER_URL;

    pagination = new Pagination();

    search = new SearchParams({ start: '', end: '' });

    list = new Array<any>();

    loading = false;

    company: any = { id: 0 };

    theads = ['#', '企业类型', '操作'];
    constructor(
        public global: GlobalService,
        private confirm: ConfirmService,
        private rechargeService: RechargeService,
        private toast: ToastService,
        private router: Router,
        private activatedRoute: ActivatedRoute,
        private modal: ModalService,
    ) { }

    ngOnInit() {
        this.activatedRoute.url
            .pipe(skipWhile(() => this.router.url !== '/company/company-page'))
            .subscribe(() => this.loadDatas());
        this.loadDatas();
    }
    loadDatas() {
        this.loading = true;
        this.rechargeService.searchypet(this.pagination, this.search).subscribe({
            next: res => {
                this.pagination.total = res.datas.total;
                this.list = res.datas.rows;
            },
            complete: () => this.loading = false
        });
    }
    confirmDelete(id) {
        this.confirm.danger('删除确认', `该企业类型下所有数据都将删除，您确认删除该企业类型信息吗？`)
            .pipe(switchMap<void, ApiData>(() => this.rechargeService.del(id)))
            .subscribe(() => {
                console.log(id);
                this.list.splice(this.list.indexOf(id), 1);
                this.toast.success('删除成功', `成功删除信息`);
                this.loadDatas();
            });
    }
    doSearch() {
        this.pagination.reset();
        this.loadDatas();
    }

    doReset() {
        const status = this.search.params.status || -1;
        this.search.clean();
        this.search.params.status = status;
        this.doSearch();
    }

    /**
     * 保存职位
     * @param banner
     */
    showEditModal(banner?: Recharge) {
        banner = banner || { id: 0, typeName: '' };
        const modal = this.modal.create(ModalComponent, { center: true });
        modal.instance.banner = banner;
        modal.open().subscribe(() => {
            this.toast.success('保存成功', '成功保存企业类型');
            this.loadDatas();
        });
    }
}
